<!DOCTYPE html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script>
      run(async function () {
        const store = testHelpers.createStore();
        const directLine = WebChat.createDirectLine({ token: await testHelpers.token.fetchDirectLineToken() });

        WebChat.renderWebChat(
          {
            directLine,

            // "yue-Hant" is not in our localization database.
            locale: 'yue-Hant',

            // "overrideLocalizedStrings" is for the developers to override existing strings.
            // We want to verify the "overrideLocalizedStrings" is sending in the default strings from "yue", which is the closest in our localization database.
            overrideLocalizedStrings: (strings, language) => {
              // The Globalize instance to use should be "yue", which is the closest in our localization database.
              expect(strings.GLOBALIZE_LANGUAGE).toBe('yue');

              return language === 'yue-Hant'
                ? {
                    ...strings,
                    TEXT_INPUT_PLACEHOLDER: strings.TEXT_INPUT_PLACEHOLDER + '？'
                  }
                : strings;
            },
            store
          },
          document.getElementById('webchat')
        );

        await pageConditions.uiConnected();

        // Verify the send box placeholder text has a trailing question mark.
        await host.snapshot();

        await pageObjects.sendMessageViaSendBox('dump-activity', { waitForSend: true });
        await pageConditions.minNumActivitiesShown(2);

        let storeState = store.getState();

        const activityFromBotYueTraditional = storeState.activities.find(({ from }) => from.role === 'bot');

        expect(activityFromBotYueTraditional).toBeTruthy();

        const { locale: activityLocaleYueTraditional } = JSON.parse(
          activityFromBotYueTraditional.attachments[0].content.replace(/```/gu, '')
        );

        // The bot should receive "yue-Hant".
        expect(activityLocaleYueTraditional).toBe('yue-Hant');

        // Web Chat Core should use "yue-Hant" when talking to the bot.
        expect(storeState.language).toBe('yue-Hant');

        // On-the-fly change the language to "en-HK" and do the same test again.
        window.WebChat.renderWebChat(
          {
            directLine,
            locale: 'en-HK',
            store
          },
          document.getElementById('webchat')
        );

        await pageObjects.sendMessageViaSendBox('dump-activity', { waitForSend: true });
        await pageConditions.minNumActivitiesShown(4);

        storeState = store.getState();

        const activityFromBotEnglishHongKong = storeState.activities.find(
          activity => activity !== activityFromBotYueTraditional && activity.from.role === 'bot'
        );

        expect(activityFromBotEnglishHongKong).toBeTruthy();

        const { locale: activityLocaleEnglishHongKong } = JSON.parse(
          activityFromBotEnglishHongKong.attachments[0].content.replace(/```/gu, '')
        );

        // The bot should receive "en-HK".
        expect(activityLocaleEnglishHongKong).toBe('en-HK');

        // Web Chat Core should use "en-HK" when talking to the bot.
        expect(storeState.language).toBe('en-HK');
      });
    </script>
  </body>
</html>
